<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeValue"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="deptTree"
          :data="depts"
          node-key="id"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @current-change="selectNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table v-loading="loading" :data="list" width="80px">
          <el-table-column label="头像" align="center">
            <template #default="{ row }">
              <img v-if="!!row.staffPhoto" :src="row.staffPhoto" class="avatar" alt="">
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" align="center" />
          <el-table-column label="手机号" prop="mobile" align="center" sortable />
          <el-table-column label="工号" prop="workNumber" align="center" sortable />
          <el-table-column label="聘用形式" align="center">
            <template v-slot:default="{ row }">
              <span v-if="row.formOfEmployment === 1">正式工</span>
              <span v-else-if="row.formOfEmployment === 2">非正式工</span>
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" align="center" />
          <el-table-column label="入职时间" prop="timeOfEntry" align="center" sortable />
          <el-table-column label="操作" align="center">
            <template #default="{ row }">
              <el-button type="text" size="mini" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="mini" @click="btnRole(row.id)">角色</el-button>
              <el-popconfirm title="确定要删除数据吗？" @onConfirm="delEmployee(row.id)">
                <template #reference>
                  <el-button style="margin-left: 10px" type="text" size="mini">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end">
          <el-pagination
            layout="total,prev,pager,next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pageSize"
            @current-change="changePage"
          />
        </el-row>
      </div>
      <import-excel :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="getEmployeeList" />
      <el-dialog :visible.sync="showRoleDialog" title="分配角色">
        <el-checkbox-group v-model="roleIds">
          <el-checkbox
            v-for="item in roleList"
            :key="item.id"
            :label="item.id"
          >{{
            item.name
          }}</el-checkbox>
        </el-checkbox-group>
        <el-row type="flex" justify="center" align="middle" style="height: 80px;">
          <el-button type="primary" size="mini" @click="btnRoleOK">确定</el-button>
          <el-button size="mini" @click="showRoleDialog = false">取消</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { assignRoleAPI, delEmployeeAPI, exportEmployeeAPI, getAllRoleEnableAPI, getDepartMentAPI, getEmployeeDetailAPI, getEmployeeListAPI } from '@/api'
import { transListToTreeData } from '@/utils'
import FileSaver from 'file-saver'
import { ImportExcel } from './components'

export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      currentUserId: null,
      showRoleDialog: false,
      roleIds: [],
      roleList: [],
      showExcelDialog: false,
      loading: false,
      list: [],
      depts: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      total: 0
    }
  },
  created() {
    this.getDepartMent()
  },
  methods: {
    async btnRole(id) {
      this.currentUserId = id
      this.roleList = await getAllRoleEnableAPI() // 获取所有可用角色
      const { roleIds } = await getEmployeeDetailAPI(id)
      this.roleIds = roleIds
      this.showRoleDialog = true
    },
    async btnRoleOK() {
      await assignRoleAPI({ roleIds: this.roleIds, id: this.currentUserId })
      this.$message.success('分配角色成功')
      this.showRoleDialog = false
    },
    async getDepartMent() {
      this.depts = transListToTreeData(await getDepartMentAPI(), 0)
      this.queryParams.departmentId = this.depts[0].id
      this.getEmployeeList()
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.queryParams.departmentId)
      })
    },
    selectNode(node) {
      this.queryParams.departmentId = node.id
      this.queryParams.page = 1
      this.getEmployeeList()
    },
    async getEmployeeList() {
      this.loading = true
      const { rows, total } = await getEmployeeListAPI(this.queryParams)
      this.list = rows
      this.total = total
      this.loading = false
    },
    changePage(newPage) {
      this.queryParams.page = newPage
      this.getEmployeeList()
    },
    changeValue() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeList()
      }, 300)
    },
    async exportEmployee() {
      const result = await exportEmployeeAPI()
      FileSaver.saveAs(result, '员工信息表.xlsx')
    },
    async delEmployee(id) {
      await delEmployeeAPI(id)
      if (this.list.length === 1) this.queryParams.page--
      this.getEmployeeList()
      this.$message.success('删除数据成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username,.avatar {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
    // .avatar{
    //   height: 30px;
    //   width: 30px;
    //   border-radius: 50%;
    // }
  }
}

</style>
